﻿//
//
/////////////////////////////////////////////////////////////////////////////

using System;
using System.Linq;
using System.Collections.Generic;
using FreeSpace.Core;
using FreeSpace.Logic;
using FreeSpaceLibrary;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;

/////////////////////////////////////////////////////////////////////////////
namespace FreeSpace.Scenes
{
    /////////////////////////////////////////////////////////////////////////
    public class MenuScene : BaseScene
    {
        private List<MenuItem> _menuItems = new List<MenuItem>();

        public delegate void StartGame(object sender, EventArgs eventArgs);
        public event StartGame OnSelectStartGameItem;

        public delegate void FinishGame(object sender, EventArgs eventArgs);
        public event FinishGame OnSelectFinishGameItem;


        /////////////////////////////////////////////////////////////////////////
        public MenuScene(SpriteBatch sprite)
            :base(sprite)
        {
            _sprite = sprite;
            _menuItems.Add(new MenuItem() { ItemType = MenuItems.StartGame, Text = "Start", FontSprite = ContentLoader.ContentFonts[FontEnum.MenuItem], Sprite = _sprite, Position = new Vector2(200, 200), Selected = true});
            _menuItems.Add(new MenuItem() { ItemType = MenuItems.FinisGame, Text = "Exit", FontSprite = ContentLoader.ContentFonts[FontEnum.MenuItem], Sprite = _sprite, Position = new Vector2(200, 300) });
        }

        /////////////////////////////////////////////////////////////////////
        public override void Update(GameTime gameTime, KeyboardState keyboardState)
        {
            base.Update(gameTime, keyboardState);

            Keys[] pressedKeys = keyboardState.GetPressedKeys();
            if (pressedKeys.Length > 0)
            {
                if (pressedKeys[0] == Keys.Up)
                {
                    _changeSelectedMenuItem(true);
                }
                else if (pressedKeys[0] == Keys.Down)
                {
                    _changeSelectedMenuItem(false);
                }

                if (pressedKeys[0] == Keys.Enter)
                {
                    //IsActive = false;
                    _resetSelectedMenuItem();
                    _processKepPress();
                    Finish();
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _processKepPress()
        {
            MenuItem selectedItem = _menuItems.FirstOrDefault(item => item.Selected);
            if (selectedItem != null)
            {
                switch (selectedItem.ItemType)
                {
                    case MenuItems.StartGame:
                        if (OnSelectStartGameItem != null)
                        {
                            OnSelectStartGameItem(this, new EventArgs());
                        }
                        break;
                    case MenuItems.FinisGame:
                        if (OnSelectFinishGameItem != null)
                        {
                            OnSelectFinishGameItem(this, new EventArgs());
                        }
                        break;
                    default:
                        return;
                }
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _changeSelectedMenuItem(bool up)
        {
            int selectedMenuIndex = 0; 
            for (int i = 0; i < _menuItems.Count; i++)
            {
                if (_menuItems[i].Selected)
                {
                    selectedMenuIndex = i;
                    break;
                }
            }
            if (up)
            {
                if (selectedMenuIndex == 0)
                {
                    return;
                }
                else
                {
                    _menuItems[selectedMenuIndex].Selected = false;
                    _menuItems[selectedMenuIndex - 1].Selected = true;
                } 
            }
            else
            {
                if (selectedMenuIndex == _menuItems.Count - 1)
                {
                    return;
                }
                else
                {
                    _menuItems[selectedMenuIndex].Selected = false;
                    _menuItems[selectedMenuIndex + 1].Selected = true;
                } 
            }
        }

        /////////////////////////////////////////////////////////////////////
        private void _resetSelectedMenuItem()
        {
            if (_menuItems.Count > 0)
            {
                foreach (MenuItem menuItem in _menuItems)
                {
                    menuItem.Selected = false;
                }
                _menuItems[0].Selected = true; 
            }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Draw()
        {
            //base.Draw();

            if (IsActive)
            {
                _sprite.Begin();
                _sprite.Draw(ContentLoader.ContentTextures[TextureEnum.BackgroundLevel1], Vector2.Zero, _tintColor);
                //_sprite.Draw(ContentLoader.ContentTextures[TextureEnum.BackgroundLevel1], Vector2.Zero, null, Color.Black, 0, Vector2.Zero, Vector2.One, SpriteEffects.None, );
                _sprite.End();

                foreach (MenuItem menuItem in _menuItems)
                {
                    menuItem.Draw();
                } 
            }
        }

        /////////////////////////////////////////////////////////////////////
        public override void Start()
        {
            base.Start();
        }
    }

    /////////////////////////////////////////////////////////////////////////
    public class MenuItem
    {
        public string Text { get; set; }
        public bool Selected { get; set; }
        public SpriteFont FontSprite { get; set; }
        public SpriteBatch Sprite { get; set; }
        public Vector2 Position { get; set; }
        public MenuItems ItemType { get; set; }

        /////////////////////////////////////////////////////////////////////
        public void Draw()
        {
            if (Sprite != null && FontSprite != null && Text != null)
            {
                Sprite.Begin();
                Sprite.DrawString(FontSprite, Text, Position, Selected ? Color.LightBlue : Color.DarkBlue);
                Sprite.End();
            }
        }
    }

    /////////////////////////////////////////////////////////////////////////
    public enum MenuItems
    {
        StartGame,
        FinisGame
    }
/////////////////////////////////////////////////////////////////////////////
}//FreeSpace.Scenes
